package com.heroh.kettle.util;

import com.heroh.kettle.constant.KettleConstant;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.logging.ChannelLogTable;
import org.pentaho.di.core.logging.JobEntryLogTable;
import org.pentaho.di.core.logging.JobLogTable;
import org.pentaho.di.core.variables.VariableSpace;
import org.pentaho.di.core.variables.Variables;
import org.pentaho.di.job.JobMeta;

/**
 *  作业日志
 *
 * @author jianshengxiong
 * @date 2019/12/31/15:45
 */
public class KettleJobLog {
    // 作业-作业日志表
    private static final String KETTLE_LOG_JOB_TABLE = "param_kettle_log_job";
    // 作业-作业项日志表
    private static final String KETTLE_LOG_JOB_ENTRY_TABLE = "param_kettle_log_job_entry";
    // 作业-日志通道日志表
    private static final String KETTLE_LOG_JOB_CHANNEL_TABLE = "param_kettle_log_job_channel";

    private static VariableSpace SPACE = null;

    private static void init(){
        // 将step日志数据库配置名加入到变量集中
        SPACE = new Variables();
        SPACE.setVariable(Const.KETTLE_TRANS_LOG_DB, KettleConstant.KETTLE_LOG_DB);
        SPACE.initializeVariablesFrom(null);
    }

    public static void setJobLog(JobMeta jobMeta) {
        init();
        // 作业日志表
        setJobLogTable(jobMeta);
        // 作业项日志表
        setJobEntryLogTable(jobMeta);
        // 日志通道日志表
        setChannelLogTable(jobMeta);
    }

    /**
     *  作业日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setJobLogTable(JobMeta jobMeta) {
        JobLogTable jobLogTable = JobLogTable.getDefault(SPACE,jobMeta);
        jobLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        jobLogTable.setTableName(KETTLE_LOG_JOB_TABLE);
        jobMeta.setJobLogTable(jobLogTable);
    }

    /**
     *  作业项日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setJobEntryLogTable(JobMeta jobMeta) {
        JobEntryLogTable jobEntryLogTable = JobEntryLogTable.getDefault(SPACE,jobMeta);
        jobEntryLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        jobEntryLogTable.setTableName(KETTLE_LOG_JOB_ENTRY_TABLE);
        jobMeta.setJobEntryLogTable(jobEntryLogTable);
    }

    /**
     *  日志通道日志表
     *
     * @author jianshengxiong
     * @date 2019/12/31/15:45
     */
    private static void setChannelLogTable(JobMeta jobMeta) {
        ChannelLogTable channelLogTable = ChannelLogTable.getDefault(SPACE,jobMeta);
        channelLogTable.setConnectionName(KettleConstant.KETTLE_LOG_DB);
        channelLogTable.setTableName(KETTLE_LOG_JOB_CHANNEL_TABLE);
        jobMeta.setChannelLogTable(channelLogTable);
    }
}
